const pool = require('../config/db');


const collectService = {
  getCollectList: async () => {
    try {
      const sql = 'select * from collect';
      const [collectList] = await pool.execute(sql);
      return collectList;
    } catch (error) {
      throw error
    }
  },
  createCollect: async (params) => {
    try {
      const sql = 'insert into collect (name, url) values (?, ?)';
      const [result] = await pool.execute(sql, [params.name, params.url]);
      if (result.affectedRows > 0) {
        return {};
      }else{
        const error = new Error('创建失败');
        error.statusCode = 400;
        throw error;
      }
    } catch (error) {
      throw error
    }
  },
  deleteCollect: async (params) => {
    try {
      const sql = 'delete from collect where id = ?';
      const [result] = await pool.execute(sql, [params.id]);
      if (result.affectedRows > 0) {
        return {};
      }else{
        const error = new Error('删除失败');
        error.statusCode = 400;
        throw error;
      }
    } catch (error) {
      throw error
    }
  },
  updateCollect: async (params) => {
    try {
      const sql = 'update collect set name = ?, url = ? where id = ?';
      const [result] = await pool.execute(sql, [params.name, params.url, params.id]);
      if (result.affectedRows > 0) {
        return {};
      }else{
        const error = new Error('更新失败');
        error.statusCode = 400;
        throw error;
      }
    } catch (error) {
      throw error
    }
  },
}

module.exports = collectService